package io.renren.wechat.authen.controller;

import io.renren.common.utils.R;
import io.renren.modules.jxgk.entity.TeacherInfoEntity;
import io.renren.modules.jxgk.service.TeacherInfoService;
import io.renren.modules.jxgkphone.entity.StudentInfo;
import io.renren.modules.jxgkphone.service.StuInfoService;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.SysUserTokenService;
import io.renren.wechat.authen.service.LoginService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/19 0019.
 */
@Controller
@RequestMapping("/wechatLogin")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    StuInfoService stuInfoService;
    @Autowired
    TeacherInfoService teacherInfoService;
    @Autowired
    SysUserTokenService sysUserTokenService;
    @RequestMapping("/Verify")
    public Map<String, Object> Verify(@RequestParam("username")String username,@RequestParam("password")String password,@RequestParam("openId")String openId){
        SysUserEntity user = sysUserService.queryByUserName(username);
        String errInfo="success";
        //账号不存在、密码错误
        if(user == null || !user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
            errInfo =("账号或密码不正确");
        }
        //账号锁定
        if(user!=null&&user.getStatus() == 0){
            errInfo =("账号已被锁定,请联系管理员");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("erroInfo",errInfo);
        StudentInfo studentInfo=stuInfoService.GetStuInfo(username);
        TeacherInfoEntity teacherInfo=teacherInfoService.getTeacherInfo(username);
        if(errInfo.equals("success")) {
            //保存用戶微信数据
            loginService.insertWecharData(username,openId);
            //生成token，并保存到数据库
            R r=sysUserTokenService.createToken(user.getUserId());
            map.put("token",r.get("token"));
            map.put("uid", username);
            if(studentInfo!=null) {
                map.put("className", studentInfo.getStudentClassName());
                map.put("studentName", studentInfo.getStudentName());
            }else {
                map.put("teacherInfo", teacherInfo);

            }map.put("email", user.getEmail());
            map.put("mobile", user.getMobile());


            List<SysMenuEntity> list=new ArrayList<SysMenuEntity>() ;
            list=sysUserService.queryAllMenus(user.getUserId());
            map.put("MenuList", list);
        }
        map.put("erroInfo",errInfo);
        return map;
    }

}
